﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Trade.DAL;
using Trade.Model;
using Trade.Controls;

namespace Trade
{
    public partial class FrmProduct : Trade.Controls.RayForm
    {
        private Product product = new Product();
        //private ProductInfo productInfo;
        private Utility.CRUD dataState = Utility.CRUD.None;
        private Common common = new Common();
        private DataTable dtProduct = null;

        public FrmProduct()
        {
            InitializeComponent();
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);

            btnOperatorGroup.btnNew.Click += new EventHandler(btnNew_Click);
            btnOperatorGroup.btnModify.Click += new EventHandler(btnModify_Click);
            btnOperatorGroup.btnDelete.Click += new EventHandler(btnDelete_Click);
            btnOperatorGroup.btnSave.Click += new EventHandler(btnSave_Click);
            btnOperatorGroup.btnCancel.Click += new EventHandler(btnCancel_Click);
            btnOperatorGroup.btnQuit.Click += new EventHandler(btnQuit_Click);
        }

        //退出
        private void btnQuit_Click(object sender, EventArgs e)
        {
            if (dataState != Utility.CRUD.None)
            {
                if (MessageBox.Show("当前数据还未保存,是否需要保存数据?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    btnOperatorGroup.btnSave.Focus();
                    return;
                }
            }

            this.Close();
        }

        //取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvProduct.Enabled = true;
            dgvProduct.Focus();
        }

        //保存
        private void btnSave_Click(object sender, EventArgs e)
        {
            int recordId = 0;

            if (String.IsNullOrEmpty(txtProductNo.Text.Trim()))
            {
                MessageBox.Show("商品编号不能为空！", "Prompt ...");
                txtProductNo.Focus();
                return;
            }

            if (String.IsNullOrEmpty(txtProductName.Text.Trim()))
            {
                if (String.IsNullOrEmpty(txtProductName.Text.Trim()))
                {
                    MessageBox.Show("商品名称不能为空！", "Prompt ...");
                    txtProductName.Focus();
                    return;
                }
            }

            ProductInfo productInfo = new ProductInfo();
            productInfo.ProductNo = txtProductNo.Text.Trim();
            productInfo.ProductName = txtProductName.Text.Trim();
            productInfo.ShortName = txtShortName.Text.Trim();
            productInfo.ProductSpec = txtProductSpec.Text.Trim();
            productInfo.ProductModel = txtProductModel.Text.Trim();
            productInfo.ProdBarcode = txtProdBarcode.Text.Trim();
            productInfo.ProdClass = (int)cmbProdClass.SelectedValue;
            productInfo.BaseUnit = (int)cmbBaseUnit.SelectedValue;
            productInfo.AidUnit = (int)cmbAidUnit.SelectedValue;
            productInfo.BaseForAid = Convert.ToInt32(txtBaseForAid.Text.Trim());
            productInfo.Place = txtPlace.Text.Trim();
            productInfo.Brand = txtBrand.Text.Trim();
            productInfo.Bopomofo = txtBopomofo.Text.Trim();
            productInfo.Remark = txtRemark.Text.Trim();
            productInfo.RecordId = dataState == Utility.CRUD.Insert ? 0 : (int)dgvProduct.CurrentRow.Cells["RecordId"].Value;

            product.WriteToDatabase(Utility.CRUDAction(dataState), productInfo, "", out recordId);

            if (dataState == Utility.CRUD.Insert)
            {
                DataRow dr = product.GetProductByRecordId(recordId);
                dtProduct.ImportRow(dr);
                dgvProduct.CurrentRow.Selected = false;
                dgvProduct.Rows[dgvProduct.RowCount - 1].Selected = true;
                dgvProduct.CurrentCell = dgvProduct.Rows[dgvProduct.RowCount - 1].Cells["ProductNo"];     //让当前选中行的Cell得到焦点
            }
            else if (dataState == Utility.CRUD.Update)
            {
                dgvProduct.CurrentRow.Cells["ProductNo"].Value = productInfo.ProductNo;
                dgvProduct.CurrentRow.Cells["ProductName"].Value = productInfo.ProductName;
                dgvProduct.CurrentRow.Cells["ShortName"].Value = productInfo.ShortName;
                dgvProduct.CurrentRow.Cells["ProductSpec"].Value = productInfo.ProductSpec;
                dgvProduct.CurrentRow.Cells["ProductModel"].Value = productInfo.ProductModel;
                dgvProduct.CurrentRow.Cells["ProdBarcode"].Value = productInfo.ProdBarcode;
                dgvProduct.CurrentRow.Cells["ProdClass"].Value = productInfo.ProdClass;
                dgvProduct.CurrentRow.Cells["BaseUnit"].Value = productInfo.BaseUnit;
                dgvProduct.CurrentRow.Cells["AidUnit"].Value = productInfo.AidUnit;
                dgvProduct.CurrentRow.Cells["BaseForAid"].Value = productInfo.BaseForAid;
                dgvProduct.CurrentRow.Cells["Place"].Value = productInfo.Place;
                dgvProduct.CurrentRow.Cells["Brand"].Value = productInfo.Brand;
                dgvProduct.CurrentRow.Cells["Bopomofo"].Value = productInfo.Bopomofo;
                dgvProduct.CurrentRow.Cells["Remark"].Value = productInfo.Remark;
                dgvProduct.CurrentRow.Cells["RecordId"].Value = productInfo.RecordId;
            }
            else if (dataState == Utility.CRUD.Delete)
            {
                dgvProduct.Rows.Remove(dgvProduct.Rows[dgvProduct.CurrentRow.Index]);
            }

            dataState = Utility.CRUD.None;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, false);
            dgvProduct.Enabled = true;
            dgvProduct.Focus();
        }

        //删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvProduct.RowCount != 0)
            {
                if (MessageBox.Show("是否真的删除当前数据?", "删除数据", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
                {
                    dataState = Utility.CRUD.Delete;
                    common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
                    dgvProduct.Enabled = false;
                }
            }
        }

        //编辑
        private void btnModify_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Update;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            ProductInfo productInfo = new ProductInfo();
            productInfo.RecordId = (int)dgvProduct.CurrentRow.Cells["RecordId"].Value;

            common.SetControlsState(this, true);
            dgvProduct.Enabled = false;
            txtProductNo.Enabled = false;
            txtProductName.Focus();
        }

        //新增
        private void btnNew_Click(object sender, EventArgs e)
        {
            dataState = Utility.CRUD.Insert;
            common.SetButtonOperatorGroup(dataState, btnOperatorGroup);
            common.SetControlsState(this, true);
            dgvProduct.Enabled = false;

            txtProductNo.Text = "";
            txtProductName.Text = "";
            txtShortName.Text = "";
            txtProductSpec.Text = "";
            txtProductModel.Text = "";
            txtProdBarcode.Text = "";
            cmbProdClass.SelectedIndex = 0;
            cmbBaseUnit.SelectedIndex = 0;
            cmbAidUnit.SelectedIndex = 0;
            txtBaseForAid.Text = "1";
            txtPlace.Text = "";
            txtBrand.Text = "";
            txtBopomofo.Text = "";
            txtRemark.Text = "";

            txtProductNo.Focus();
        }

        private void FrmProduct_Load(object sender, EventArgs e)
        {
            dtProduct = product.GetProductList();

            dgvProduct.DataSource = dtProduct;
            dgvProduct.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            SetProductDataGridView();

            cmbProdClass.Tag = "G000";
            cmbBaseUnit.Tag = "U000";
            cmbAidUnit.Tag = "U000";

            common.ComboBoxBindingDataSource(cmbProdClass, "SubjectName", "SubjectId");
            common.ComboBoxBindingDataSource(cmbBaseUnit, "SubjectName", "SubjectId");
            common.ComboBoxBindingDataSource(cmbAidUnit, "SubjectName", "SubjectId");

            if (dgvProduct.RowCount != 0)
            {
                dgvProduct.CurrentRow.Selected = true;
            }
            dgvProduct.Focus();
        }

        private void SetProductDataGridView()
        {
            dgvProduct.AllowUserToAddRows = false;
            dgvProduct.AllowUserToDeleteRows = false;
            dgvProduct.ReadOnly = true;

            dgvProduct.Columns[0].Width = 80;
            dgvProduct.Columns[0].Name = dtProduct.Columns["ProductNo"].ColumnName.ToString();
            dgvProduct.Columns[0].HeaderText = "商品编号";
            dgvProduct.Columns[1].Width = 150;
            dgvProduct.Columns[1].Name = dtProduct.Columns["ProductName"].ColumnName.ToString();
            dgvProduct.Columns[1].HeaderText = "品名";
            dgvProduct.Columns[2].Width = 80;
            dgvProduct.Columns[2].Name = dtProduct.Columns["ShortName"].ColumnName.ToString();
            dgvProduct.Columns[2].HeaderText = "简称";
            dgvProduct.Columns[3].Width = 150;
            dgvProduct.Columns[3].Name = dtProduct.Columns["ProductSpec"].ColumnName.ToString();
            dgvProduct.Columns[3].HeaderText = "规格";
            dgvProduct.Columns[4].Width = 150;
            dgvProduct.Columns[4].Name = dtProduct.Columns["ProductModel"].ColumnName.ToString();
            dgvProduct.Columns[4].HeaderText = "型号";
            dgvProduct.Columns[5].Width = 150;
            dgvProduct.Columns[5].Name = dtProduct.Columns["ProdBarcode"].ColumnName.ToString();
            dgvProduct.Columns[5].HeaderText = "条码";
            dgvProduct.Columns[6].Width = 80;
            dgvProduct.Columns[6].Name = dtProduct.Columns["ProdClass"].ColumnName.ToString();
            dgvProduct.Columns[6].HeaderText = "分类";
            dgvProduct.Columns[6].Visible = false;
            dgvProduct.Columns[7].Width = 80;
            dgvProduct.Columns[7].Name = dtProduct.Columns["ProdClassDes"].ColumnName.ToString();
            dgvProduct.Columns[7].HeaderText = "类别";
            dgvProduct.Columns[8].Width = 80;
            dgvProduct.Columns[8].Name = dtProduct.Columns["BaseUnit"].ColumnName.ToString();
            dgvProduct.Columns[8].HeaderText = "基本";
            dgvProduct.Columns[8].Visible = false;
            dgvProduct.Columns[9].Width = 80;
            dgvProduct.Columns[9].Name = dtProduct.Columns["BaseUnitDes"].ColumnName.ToString();
            dgvProduct.Columns[9].HeaderText = "基本单位";
            dgvProduct.Columns[10].Width = 80;
            dgvProduct.Columns[10].Name = dtProduct.Columns["AidUnit"].ColumnName.ToString();
            dgvProduct.Columns[10].HeaderText = "辅助";
            dgvProduct.Columns[10].Visible = false;
            dgvProduct.Columns[11].Width = 80;
            dgvProduct.Columns[11].Name = dtProduct.Columns["AidUnitDes"].ColumnName.ToString();
            dgvProduct.Columns[11].HeaderText = "辅助单位";
            dgvProduct.Columns[12].Width = 80;
            dgvProduct.Columns[12].Name = dtProduct.Columns["BaseForAid"].ColumnName.ToString();
            dgvProduct.Columns[12].HeaderText = "单位对应关系";
            dgvProduct.Columns[13].Width = 120;
            dgvProduct.Columns[13].Name = dtProduct.Columns["Place"].ColumnName.ToString();
            dgvProduct.Columns[13].HeaderText = "产地";
            dgvProduct.Columns[14].Width = 120;
            dgvProduct.Columns[14].Name = dtProduct.Columns["Brand"].ColumnName.ToString();
            dgvProduct.Columns[14].HeaderText = "品牌";
            dgvProduct.Columns[15].Width = 80;
            dgvProduct.Columns[15].Name = dtProduct.Columns["Bopomofo"].ColumnName.ToString();
            dgvProduct.Columns[15].HeaderText = "拼音编码";
            dgvProduct.Columns[16].Width = 200;
            dgvProduct.Columns[16].Name = dtProduct.Columns["Remark"].ColumnName.ToString();
            dgvProduct.Columns[16].HeaderText = "备注";
            dgvProduct.Columns[17].Width = 80;
            dgvProduct.Columns[17].Name = dtProduct.Columns["RecordId"].ColumnName.ToString();
            dgvProduct.Columns[17].HeaderText = "记录编号";
            dgvProduct.Columns[17].Visible = false;

            dgvProduct.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }
    }
}
